import os
import requests
from dotenv import load_dotenv


# 加载环境变量
load_dotenv()

class DeepSeekAPI:
    def __init__(self):
        self.base_url = os.getenv("DEEPSEEK_API_BASEURL")
        self.api_key = os.getenv("DEEPSEEK_API_KEY")

    def create_chat_completion(self, prompt, model="deepseek-chat", temperature=0.7):
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.api_key}"
        }

        payload = {
            "model": model,
            "messages": [
                {"role": "user", "content": prompt}
            ],
            "temperature": temperature
        }

        try:
            response = requests.post(self.base_url, json=payload, headers=headers)
            response.raise_for_status()
            return response.json()["choices"][0]["message"]["content"]
        except requests.exceptions.RequestException as e:
            print(f"API请求失败: {str(e)}")
            return None

# 使用示例
if __name__ == "__main__":
    deepseek = DeepSeekAPI()
    response = deepseek.create_chat_completion("早上好！")
    print("DeepSeek 响应：")
    print(response)
